package com.skzh.web.controller.system;

import com.skzh.common.annotation.Log;
import com.skzh.common.constant.UserConstants;
import com.skzh.common.core.controller.BaseController;
import com.skzh.common.core.domain.AjaxResult;
import com.skzh.common.core.domain.entity.SysRegion;
import com.skzh.common.enums.BusinessType;
import com.skzh.common.utils.StringUtils;
import com.skzh.system.service.ISysRegionService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 区域信息
 *
 * @author skzh
 */
@RestController
@RequestMapping("/system/region" )
public class SysRegionController extends BaseController {
    @Autowired
    private ISysRegionService regionService;

    /**
     * 获取区域列表
     */
    @PreAuthorize("@ss.hasPermi('system:region:list')" )
    @GetMapping("/list" )
    public AjaxResult list(SysRegion region) {
        List<SysRegion> regions = regionService.selectRegionList(region);
        return success(regions);
    }

    /**
     * 查询区域列表（排除节点）
     */
    @PreAuthorize("@ss.hasPermi('system:region:list')" )
    @GetMapping("/list/exclude/{regionId}" )
    public AjaxResult excludeChild(@PathVariable(value = "regionId" , required = false) Long regionId) {
        List<SysRegion> regions = regionService.selectRegionList(new SysRegion());
        regions.removeIf(d -> d.getRegionId().intValue() == regionId || ArrayUtils.contains(StringUtils.split(d.getAncestors(), "," ), regionId + "" ));
        return success(regions);
    }

    /**
     * 根据区域编号获取详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:region:query')" )
    @GetMapping(value = "/{regionId}" )
    public AjaxResult getInfo(@PathVariable Long regionId) {
        regionService.checkRegionDataScope(regionId);
        return success(regionService.selectRegionById(regionId));
    }

    /**
     * 新增区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:add')" )
    @Log(title = "区域管理" , businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysRegion region) {
        if (!regionService.checkRegionNameUnique(region)) {
            return error("新增区域'" + region.getRegionName() + "'失败，区域名称已存在" );
        }
        region.setCreateBy(getUsername());
        return toAjax(regionService.insertRegion(region));
    }

    /**
     * 修改区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:edit')" )
    @Log(title = "区域管理" , businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody SysRegion region) {
        Long regionId = region.getRegionId();
        regionService.checkRegionDataScope(regionId);
        if (!regionService.checkRegionNameUnique(region)) {
            return error("修改区域'" + region.getRegionName() + "'失败，区域名称已存在" );
        } else if (region.getParentId().equals(regionId)) {
            return error("修改区域'" + region.getRegionName() + "'失败，上级区域不能是自己" );
        } else if (StringUtils.equals(UserConstants.REGION_DISABLE, region.getStatus()) && regionService.selectNormalChildrenRegionById(regionId) > 0) {
            return error("该区域包含未停用的子区域！" );
        }
        region.setUpdateBy(getUsername());
        return toAjax(regionService.updateRegion(region));
    }

    /**
     * 删除区域
     */
    @PreAuthorize("@ss.hasPermi('system:region:remove')" )
    @Log(title = "区域管理" , businessType = BusinessType.DELETE)
    @DeleteMapping("/{regionId}" )
    public AjaxResult remove(@PathVariable Long regionId) {
        if (regionService.hasChildByRegionId(regionId)) {
            return warn("存在下级区域,不允许删除" );
        }
        if (regionService.checkRegionExistUser(regionId)) {
            return warn("区域存在用户,不允许删除" );
        }
        regionService.checkRegionDataScope(regionId);
        return toAjax(regionService.deleteRegionById(regionId));
    }
}
